//
//  CASelectDistrictViewController.m
//  LNH
//
//  Created by twentylx on 2018/2/26.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CASelectDistrictViewController.h"
#import "CAAddressPickerView.h"
#import "CASelectDistrictView.h"
#import "CACityModel.h"
#import "CAProvinceApi.h"
#import "CACheckAgencyApi.h"
#import "CAUpgradeGoodsViewController.h"

@interface CASelectDistrictViewController () {
    NSString *_districtId;
}

@property (nonatomic, strong) CASelectDistrictView *selectDistrictView;
@property (nonatomic, strong) CAAddressPickerView *pickerView;

@end

@implementation CASelectDistrictViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 初始化数据
- (void)lm_initialDefaultsForController {
    [self loadAddress];
}

/// 绑定 vm
- (void)lm_bindViewModelForController {
    
}

/// 创建视图
- (void)lm_createViewForController {
    [self.view setNeedsUpdateConstraints];
    
    [self.view addSubview:self.selectDistrictView];
    [self.selectDistrictView addSubview:self.pickerView];
}

/// 配置导航栏
- (void)lm_configNavigationForController {
    self.navigationItem.title = @"选择地区";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:self action:@selector(nextAction)];
}

#pragma mark layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
    
    [_selectDistrictView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [_pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.selectDistrictView);
        make.bottom.equalTo(self.selectDistrictView);
        make.height.mas_equalTo(250);
    }];
    
}

#pragma mark get & set
- (CASelectDistrictView *)selectDistrictView {
    if (!_selectDistrictView) {
        _selectDistrictView = [CASelectDistrictView new];
        
        UITapGestureRecognizer *gesture1 = [UITapGestureRecognizer new];
        [_selectDistrictView addGestureRecognizer:gesture1];
        [[gesture1 rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
//            [self.pickerView reloadAllComponents];
            self.pickerView.hidden = YES;
        }];
        UITapGestureRecognizer *gesture = [UITapGestureRecognizer new];
        [_selectDistrictView.districtView addGestureRecognizer:gesture];
        [[gesture rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            [self.pickerView reloadAllComponents];
            self.pickerView.hidden = NO;
        }];
    }
    return _selectDistrictView;
}

-(CAAddressPickerView *) pickerView {
    if (!_pickerView) {
        _pickerView = [[CAAddressPickerView alloc] initWithFrame:CGRectZero];
        _pickerView.hidden = YES;
        
        [_pickerView.selecedCitySubject subscribeNext:^(CACityModel *cityModel) {
            if ([self.levelId isEqualToString:@"14"]) {
                _districtId = [NSString stringWithFormat:@"%ld", [cityModel.regionId integerValue]];;
                [self.selectDistrictView.districtLabel setText:[NSString stringWithFormat:@"%@ %@ %@", cityModel.province, cityModel.city, cityModel.region]];
            } else if ([self.levelId isEqualToString:@"15"]) {
                _districtId = [NSString stringWithFormat:@"%ld", [cityModel.cityId integerValue]];
                [self.selectDistrictView.districtLabel setText:[NSString stringWithFormat:@"%@ %@", cityModel.province, cityModel.city]];
            } else if ([self.levelId isEqualToString:@"16"]){
                _districtId = [NSString stringWithFormat:@"%ld", [cityModel.provinceId integerValue]];
                [self.selectDistrictView.districtLabel setText:[NSString stringWithFormat:@"%@=", cityModel.province]];
            }
            [self checkAgencyAction:_districtId];
        }];
    }
    return _pickerView;
}

#pragma mark private method
- (void) loadAddress {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    CAProvinceApi *provinceApi = [[CAProvinceApi alloc] init];
    [provinceApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CAProvinceApi *api = (CAProvinceApi *) request;
        if (api.done) {
            self.pickerView.localArray = api.responseObject[@"result"];
            [self.pickerView getPickerData];
        } else {
            [SVProgressHUD showErrorWithStatus:api.errorMsg];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
    }];
}

- (void) checkAgencyAction:(NSString *) disctrictId {
    CACheckAgencyApi *checkAgencyApi = [[CACheckAgencyApi alloc] initWithLevel:self.levelId Remark:disctrictId];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [checkAgencyApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CACheckAgencyApi *api = (CACheckAgencyApi *) request;
        if (api.done) {
            NSLog(@"%@", api.responseObject);
        } else {
            [SVProgressHUD lm_displayErrorWithStatus:api.errorMsg];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
    }];
}

- (void) nextAction {
    if (IsStrEmpty(_districtId)) {
        [SVProgressHUD lm_dispalyInfoWithStatus:@"请选择区域"];
        return;
    }
    CAUpgradeGoodsViewController *upgradeGoodsVC = [CAUpgradeGoodsViewController new];
    upgradeGoodsVC.level = self.levelId;
    upgradeGoodsVC.districtId = _districtId;
    [self.navigationController pushViewController:upgradeGoodsVC animated:YES];
}

@end
